package wexin.web.controll;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import wexin.web.jpa.domain.Article;
import wexin.web.jpa.domain.ArticleType;
import wexin.web.jpa.service.ArticleService;
import wexin.web.util.PagenationHelper;

@Controller
@RequestMapping("/article")
public class ArticleController {


	private static final int DEFAULT_PAGEABLE_SIZE = 10;

	@Autowired
	private ArticleService articleService;

	@RequestMapping(value = "/show", method = { RequestMethod.GET, RequestMethod.POST })
	public String show(
			/*@PathVariable("type") String type,*/
			@PageableDefault(page = 0, size = DEFAULT_PAGEABLE_SIZE) Pageable pageable,
			Model model) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start_line", pageable.getOffset() + 1);
		map.put("end_line", (pageable.getPageNumber() + 1) * pageable.getPageSize());
		map.put("orderByClause", "username asc");
		ArticleType articleType = ArticleType.valueOf("ORIGINAL");
		Page<Article> page = articleService.findByType(articleType, pageable);
		PagenationHelper ph = new PagenationHelper(page.getNumber(), page.getSize(), page.getTotalPages());
		model.addAttribute("page", page);
		model.addAttribute("ph", ph);

		return "show";
	}
	
	@RequestMapping(value = "/pagethree", method = { RequestMethod.GET, RequestMethod.POST })
	public String pagethree(
			@PageableDefault(page = 0, size = DEFAULT_PAGEABLE_SIZE) Pageable pageable,
			Model model) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start_line", pageable.getOffset() + 1);
		map.put("end_line", (pageable.getPageNumber() + 1) * pageable.getPageSize());
		map.put("orderByClause", "username asc");
		ArticleType articleType = ArticleType.valueOf("ORIGINAL");
		Page<Article> page = articleService.findByType(articleType, pageable);
		PagenationHelper ph = new PagenationHelper(page.getNumber(), page.getSize(), page.getTotalPages());
		model.addAttribute("page", page);
		model.addAttribute("ph", ph);

		return "pagethree";
	}
	
	@RequestMapping(value = "/pagetwo", method = { RequestMethod.GET, RequestMethod.POST })
	public String pagetwo(
			@PageableDefault(page = 0, size = 5) Pageable pageable,
			Model model) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start_line", pageable.getOffset() + 1);
		map.put("end_line", (pageable.getPageNumber() + 1) * pageable.getPageSize());
		map.put("orderByClause", "username asc");
		ArticleType articleType = ArticleType.valueOf("ORIGINAL");
		Page<Article> page = articleService.findByType(articleType, pageable);
		PagenationHelper ph = new PagenationHelper(page.getNumber(), page.getSize(), page.getTotalPages());
		model.addAttribute("page", page);
		model.addAttribute("ph", ph);

		return "pagetwo";
	}
	
	@RequestMapping(value = "/pageone", method = { RequestMethod.GET, RequestMethod.POST })
	public String pageone(
			@PageableDefault(page = 0, size = 3) Pageable pageable,
			Model model) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start_line", pageable.getOffset() + 1);
		map.put("end_line", (pageable.getPageNumber() + 1) * pageable.getPageSize());
		map.put("orderByClause", "username asc");
		ArticleType articleType = ArticleType.valueOf("ORIGINAL");
		Page<Article> page = articleService.findByType(articleType, pageable);
		PagenationHelper ph = new PagenationHelper(page.getNumber(), page.getSize(), page.getTotalPages());
		model.addAttribute("page", page);
		model.addAttribute("ph", ph);

		return "pageone";
	}

}
